<?php
namespace Home\Controller;
use Common\Lib\User;
use Home\Model\DepartmentModel;
use Home\Model\RoleModel;

class MyApprovalController extends CommonController {
	protected $table="ApplicationApproval";
	protected $pageSize = 13;


	public function index (){
		$D = D('RbacUser');
		$this->assign('start', I('get.start',''));
        $this->assign('end',   I('get.end',''));
        /*$this->assign("statusType",array(array("id"=>"1","name"=>"通过"),array("id"=>"2","name"=>"不通过")));*/
		//获取所有CRM部门 (一维)
        $getalldepartmentsList_url = "Department/getAllDepartmentsList";
	    $alldepartmentsList = json_decode(http_get($getalldepartmentsList_url),true);
		//获取所有CRM部门 (二维)
        $getalldepartments_url = "Department/getAllDepartments";
	    $alldepartments = json_decode(http_get($getalldepartments_url),true);
		$ename = $this->getRoleEname();
    	$this->assign('viewDecorator', D("Department")->decoratorView($ename));
    	//部门选项权限
    	$ename=$this->getRoleEname();
    	//所属部门
    	$departmentInfo = D("Department")->getDepartment(session('account')['userInfo']['department_id']);
        $arr=$departmentInfo["list"];
        if($ename==RoleModel::GOLD||$ename==RoleModel::HR_MASTER || session('account')['userInfo']['department_id']==22){
        	 $departments=array('list'=>$alldepartments["list"],'account'=>'','group'=>'');    
        }else{
        	    //根据部门id获取所有组 (二维)
        	    $department_id=session('account')['userInfo']['department_id'];
        	    $field = "id,name";
			    $getgroupById_url = "Group/getGroupById?id=".$department_id."&field=".$field;
			    $crm_groups = json_decode(http_get($getgroupById_url),true);
                $departments=array('list'=>$arr,'account'=>$arr,'group'=>$crm_groups);
        }
        $userNameList = M("RbacUser")->join('user_info ON rbac_user.id = user_info.user_id')->getField('user_id,realname', true);

        $this->assign('departments',$departments);
    	//获取crm的部门，职能 start
    	$this->assign('departmentsList',$alldepartmentsList["list"]);//一维
    	$this->assign('departmentsType',$alldepartments["list"]);//二维
    	//获取crm的部门，职能 end
		$this->assign("paymentMethodList",   $this->M->getPaymentMethodList(null));
		$this->assign('paymentMethodType',$this->M->getPaymentMethodType());//二维
		$this->assign("departmentNameList", D("Department")->getDepartmentsNames());
		$this->assign('userNameList',$userNameList);//一维
		$this->assign('statusList',$this->M->getStatusList(null));//一维
		$this->assign('statusType',$this->M->getStatusType());//二维
		$this->assign('applicationTypeList',$this->M->getApplicationTypeList(null));//一维
		$this->assign('applicationTypeType',$this->M->getApplicationTypeType());//二维
		$this->display();
	}

	public function setQeuryCondition() {
			$department_id = I('get.department_id');
			$user_id = session('account')['userInfo']['user_id'];  
			$this->M->where(array('department_id'=>session('account')['userInfo']['department_id'],'user_id'=>$user_id))
			        ->order("id desc");
			if (isset($_GET['name'])) {
			$this->M->where(array('name'=>array('like', I('get.name')."%")));
		    }
            if($_GET["start"]!=''&&$_GET["end"]!=''){
            	$start = $_GET["start"]." 00:00:00" ;
            	$end = $_GET["end"]." 00:00:00" ;
            	$this->M->where(array('created_at'=> array(array('EGT',$start),array('ELT',$end))));
            }
            if (isset($_GET['approval_status'])) {
		    		$this->M->where(array('approval_status'=>$_GET['approval_status']));
            }
            if (isset($_GET['application_type'])) {
		    		$this->M->where(array('application_type'=>$_GET['application_type']));
            }
	}
	/**
	* 添加
	*/
	public function add(){
		$_POST["expected_date"] = empty($_POST["expected_date"])?"":UTC_to_locale_time($_POST["expected_date"]);
	    $_POST['department_id'] = session('account')['userInfo']['department_id'];
	    $_POST['user_id'] = session('account')['userInfo']['user_id'];
		$re = $this->M->create($_POST, 1);
		if ($re) {
			$this->M->startTrans(); 
			$re['applicationApproval'] = M('applicationApproval')->create($_POST, 1);
			$id = $this->M->add($re);
			if ($id) {
					$this->M->commit();
					$this->success(L('ADD_SUCCESS'));
			} else {
				$this->M->rollback();
				$this->error($this->M->getError());
			}
		} else {
			$this->error($this->M->getError());
		}

	}

	/**
	* 编辑
	*/
	public function edit(){
		/*echo 'ss';
		print_r($_POST);
		echo 'kk';
		echo 'gg';*/
		$_POST["expected_date"] = empty($_POST["expected_date"])?"":UTC_to_locale_time($_POST["expected_date"]);
        $re = $this->M->create($_POST);
		if ($re) {
			if ($this->M->where(array('id'=>I('post.id') ))->save() !== false) {
				$this->success(L('ADD_SUCCESS'));
			} else {
				$this->error($this->M->getError());
			}
		} else {
			$this->error($this->M->getError());
		}

	}

	public function setApprovalStatus(){
	
		$id = $_POST["id"];
		$arr = M("ApplicationApproval")->field("approval_id")->where(array('id'=>$id))->select();
		if($arr["0"]["approval_id"]!=session('account')['userInfo']['user_id'] && $arr["0"]["approval_id"]!=0){
			$this->error("该申请已经被审批，修改审批状态只能由审批该申请的人来修改！");
		}
	
		$_POST["approval_date"] = date("Y-m-d");
		$_POST["approval_id"] = session('account')['userInfo']['user_id'];
		$re = $this->M->create($_POST, 2);
		if ($re) {
			if ($this->M->save() !== false) {
				$this->success(L('ADD_SUCCESS'));
			} else {
				$this->error($this->M->getError().$this->M->getLastSql());
			}
		} else {
			$this->error($this->M->getError().$this->M->getLastSql());
		}
		
	}




}
